//
//  BN_ChatHostPageCell.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-5-13.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_ChatHostPageCell.h"

@implementation BN_ChatHostPageCell

- (void)awakeFromNib
{
    // UI
    self.fengeLine.alpha = 0.4;
    self.userContent.numberOfLines = 0;
    [self addObserver:self forKeyPath:@"newsStr" options:NSKeyValueObservingOptionNew context:NULL];
    _userName.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:16];
    
    // TAP
    self.userPhoto.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(lookPersonInfo)];
    [self.userPhoto addGestureRecognizer:singleTap];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    
    for (UIView *view in self.tishiImgView.subviews) {
        [view removeFromSuperview];
    }
    
    NSDictionary *newDict = change;
    NSString *newStr = newDict[@"new"];
    if (![newStr isEqualToString:@"0"]) {
        self.tishiImgView.hidden = NO;
        UILabel *numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
        
        // 处理未读99条以后
        NSInteger numberInt = [newStr integerValue];
        if (numberInt > 99) {
            numberLabel.text = @"99+";
            numberLabel.font = [UIFont systemFontOfSize:8];
        }else {
            numberLabel.text = newStr;
            numberLabel.font = [UIFont systemFontOfSize:9];
        }
        numberLabel.textAlignment = NSTextAlignmentCenter;
        numberLabel.textColor = [UIColor whiteColor];
        [self.tishiImgView addSubview:numberLabel];
    }else {
        self.tishiImgView.hidden = YES;
    }
}

- (void)lookPersonInfo {
    [[NSNotificationCenter defaultCenter]postNotificationName:K_CHAT_LOOK_PERSONALINFO object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:K_CHAT_LOOK_PERSONALINFO object:nil];
    [self removeObserver:self forKeyPath:@"newsStr" context:NULL];
}
@end
